﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using DataAccessLayer;

namespace ChamCong.Admin
{
    public partial class QuanLyLoaiPhep : BasePage
    {
        LoaiPhepBLL lpBLL = new LoaiPhepBLL();
        LoaiCongBLL lcBLL = new LoaiCongBLL();
        public int _MaLP;
        public string _LoaiCong;
        protected void Page_Load(object sender, EventArgs e)
        {
            _MaLP = Convert.ToInt32(Request.QueryString["mlp"]);
            try
            {
                if (!Page.IsPostBack)
                {
                    HienThiMenu();
                    bindDdlLoaiCong();
                    if (_MaLP > 0)
                    {
                        rdbMenu.Items[0].Selected = true;
                        _LoaiCong = lpBLL.LayMaLCTheoLoaiPhep(_MaLP);
                        for (int i = 0; i < ddlLoaiCong.Items.Count; i++)
                        {
                            if (_LoaiCong == ddlLoaiCong.Items[i].Value.ToString())
                            {
                                ddlLoaiCong.Items[i].Selected = true;
                            }
                        }
                        txtLoaiPhep.Text = lpBLL.LayLoaiPhepTheMaLP(_MaLP).TenLoaiPhep;
                        btnCapNhat.Visible = true;
                        btnXoa.Visible = true;
                    }
                }
            }
            catch
            {
                Response.Redirect("QuanLyLoaiPhep.aspx");
                
            }

        }
        void bindDdlLoaiCong()
        {
            ddlLoaiCong.DataSource = lcBLL.LayTatCa();
            ddlLoaiCong.DataTextField = "TenCong";
            ddlLoaiCong.DataValueField = "MaLoaiCong";
            ddlLoaiCong.DataBind();


        }
        void HienThiMenu()
        {
            List<LOAIPHEP> Cap1 = lpBLL.LayTatCa();

            foreach (var item in Cap1)
            {
                TreeNode child = new TreeNode(item.TenLoaiPhep, item.MaLoaiPhep.ToString());
                child.NavigateUrl = "QuanLyLoaiPhep.aspx?mlp=" + item.MaLoaiPhep;
                child.ToolTip = item.TenLoaiPhep.ToString();
                tvwLoaiPhep.Nodes.Add(child);
            }

        }

        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                LOAIPHEP lp = lpBLL.LayLoaiPhepTheMaLP(_MaLP);
                lp.TenLoaiPhep = txtLoaiPhep.Text;
                lp.MaLoaiCong = ddlLoaiCong.SelectedValue;
                lpBLL.SuaLoaiPhep(lp);
                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Success, "Thông báo", "Sửa loại phép thành công!");
            }
            catch (Exception)
            {

                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", "Lỗi - Không thể sửa!");
            }

        }

        protected void rdbMenu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rdbMenu.SelectedValue == "0")
            {
                btnThemMoi.Visible = false;
            }
            if (rdbMenu.SelectedValue == "1")
            {
                btnCapNhat.Visible = false;
                btnThemMoi.Visible = true;
                txtLoaiPhep.Text = "";
            }
        }

        protected void btnThemMoi_Click(object sender, EventArgs e)
        {
            try
            {
                if (lpBLL.kiemTraTenTonTai(txtLoaiPhep.Text) == null)
                {
                    LOAIPHEP lp = new LOAIPHEP();
                    lp.TenLoaiPhep = txtLoaiPhep.Text;
                    lp.MaLoaiCong = ddlLoaiCong.SelectedValue;
                    lpBLL.themLoaiPhep(lp);
                    sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Success, "Thông báo", "Thêm loại phép thành công!");
                }
                else
                {
                    sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", lpBLL.kiemTraTenTonTai(txtLoaiPhep.Text) + " đã tồn tại!");
                }
               
            }
            catch (Exception ex)
            {

                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", "Lỗi " + ex.Message);
            }

        }

        protected void btnXoa_Click(object sender, EventArgs e)
        {
            lpBLL.XoaLoaiPhep(_MaLP);
            
            Response.Redirect("QuanLyLoaiPhep.aspx");
            sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Success, "Thông báo", "Xóa loại phép thành công!");
        }
    }
}